from ConfigParser import ConfigParser
from numpy import matrix
import wx
import os

##  Reads the prefences from the preference file in which the location of the stylus, calibration and bony landmark position files are stored.
class PreferencesFileReader:

    ##  The constructor of PreferencesFileReader.
    #   @param[in]  gui GUI instance.
    def __init__(self,gui):
        ## GUI instance.
        self.gui = gui
        ##  The location of the preferences file.
        self.preferencesFile = os.path.join('Preferences', 'preferences.ini')
        config = ConfigParser()

        try:
            config.read(self.preferencesFile)
            ##  Location of the stylusFile, contains data for the pointer sensor.
            #   Default location: Data\Stylus.dat.
            self.stylusFile = config.get("Preferences", "stylusFile")
            ##  Location of the calibration file, containing data concerning calibration data for the elektromagnetic field of the Flock of Birds system.
            #   Default location: Data\Calibration.dat.
            self.calibrationFile = config.get("Preferences", "calibrationFile")
            ##  Location of the bony landmark position file, containing coordinates for the bony landmarks in the bone models.
            #  Default location:  \BLPositions.ini.
            self.blPositionsFile = config.get("Preferences", "blPositionsFile")
            ##  Location of the cofiguration file, containing sensor and bony landmark configurations.
            #  Default location:  \config.ini.
            self.configFile = config.get("Preferences", "configFile")
        except:
            msg = "Could not read the preferences file. Please check the settings in %s for inconsistencies." % self.preferencesFile
            self.DisplayErrorDialog(msg)

    ##  Parses the calibration file.
    #   @param[in]  fileName    The location of the calibration file.
    #   @return[matrix]         The calibration file in matrix representation.
    def ReadCalibration(self, fileName):

        try:
            datFile = open(fileName,'r')
            fullString = "[" + datFile.read().replace('\n', ';') + "]"
        except:
            msg = "Could not read the calibration file. Please check the settings in %s for inconsistencies." % fileName
            self.DisplayErrorDialog(msg)

        return matrix(fullString)
    
    ## Saves the system preferences files.
    #  @return[bool] Returns a boolean value representing a system reset.
    def SavePreferences(self, dlg):

        try:
            file = open(self.preferencesFile, 'w')
            file.write("[Preferences]\n")
            file.write("stylusFile: %s\n" % dlg.stylusFile.GetValue())
            file.write("calibrationFile: %s\n" % dlg.calibrationFile.GetValue())
            file.write("blPositionsFile: %s\n"% dlg.blPositionsFile.GetValue())
            file.write("configFile: %s\n"% dlg.configFile.GetValue())
            file.close()
        except:
            msg = "An error occured while writing to the preferences file %" % self.preferencesFile
            self.DisplayErrorDiload(msg)
        
    ## Displays an error dialog.
    # @param[in]    msg The message to be displayed.
    def DisplayErrorDialog(self, msg):
        dlg = wx.MessageDialog(self.gui, msg, 'Warning', wx.OK | wx.ICON_EXCLAMATION)
        dlg.ShowModal()